package controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.ext.plugin.shiro.ShiroKit;
import config.validator.LoginValidator;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;

/**
 * Created by Administrator on 2016/12/29.
 */
public class LoginController extends Controller{


    public void login(){
        String error = null;
        String rememberMe = "true";
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(getPara("user.name"),getPara("user.pass"));
        if(rememberMe != null && "true".equals(rememberMe))
            token.setRememberMe(false);    // 记住我
        try {
            subject.login(token);
        } catch (UnknownAccountException | IncorrectCredentialsException e1) {
            error = "用户名或密码错误";
        }catch(ExcessiveAttemptsException e){
            //userService.lockAccountByNo(no);
            System.out.println("锁定账户");// 锁定账户
            error = "超过了尝试登录的次数，您的账户已经被锁定。";
        }catch (AuthenticationException e) {    // 其他错误
            if(e.getMessage() != null)
                error = "发生错误：" + e.getMessage();
            else
                error = "发生错误，无法登录。";
        }
        System.out.println(error);
        //renderText("登录成功");
        render(ShiroKit.getSuccessUrl());
    }


    public void success(){
        Subject subject = SecurityUtils.getSubject();

        System.out.println("登陆成功");
        renderText("登陆成功");
    }


    public void unauthorized(){
        System.out.println("未授权");
        renderText("未授权");
    }

}
